package com.example.tool.action.controls;

import com.example.tool.action.ActionUtil;
import com.example.tool.ui.HToolIcon;
import com.example.tool.utils.FileUtil;
import com.example.tool.utils.SwingUtil;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFileManager;
import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * <p>Title: CAP4ControlsActions</p>
 * Description: CAP4自定义表单控件
 * <p>CreateTime: 2022/6/27 18:10</p>
 * @author FT
 */
@SuppressWarnings("Duplicates")
public class CAP4ControlsActions extends AnAction {

  @Override
  public @NotNull ActionUpdateThread getActionUpdateThread() {
    return super.getActionUpdateThread();
  }

  @Override
  public void actionPerformed(@NotNull AnActionEvent e) {
    String[] configInformation = ActionUtil.getInformation(e.getProject());
    if (configInformation != null) {
      String pcFile = "/" + configInformation[2] + "/apps_res/" + configInformation[1] + "/controls/js/";
      String mobileFile = "/" + configInformation[2] + "/m3/apps/v5/meeting/" + configInformation[1] + "/js";
      SwingUtil.setTips("<span style=\"color: #F56C6C;\">不同版本可能会有偏差，js文件与类名一致。</span><br>创建的 <span style=\"color: #F56C6C;\">PC端控件js文件</span> 将会放在以下路径中。<br><span style=\"color: #F56C6C;\">" + pcFile + "</span><br>创建的 <span style=\"color: #F56C6C;\">移动端控件js文件</span> 将会放在以下路径中。<br><span style=\"color: #F56C6C;\">" + mobileFile + "</span>");
      ActionUtil.ActionCreatInput(e, (information, value) -> {
        String className = value.get(0);
        String controlId = value.get(1);
        String controlName = value.get(2);
        // xx.java
        FileUtil.generateFile(information[0] + "/" + className + ".java", "/demo/init/controls/cap4/CAP4PlusControl.txt",
            str -> str.replace("$package$", information[1])
                .replace("$jsPath$", configInformation[1] + "/js/" + className + ".js")
                .replace("$pluginId$", configInformation[1])
                .replace("$ClassName$", className)
                .replace("$JsName$", className)
                .replace("$controlId$", controlId)
                .replace("$controlName$", controlName)
                .replace("$TIME$", new SimpleDateFormat("yyyy-MM-dd").format(new Date())));

        // xxPage.html
        String configHtml = Objects.requireNonNull(e.getProject()).getBasePath() + "/" + configInformation[2] + "/apps_res/cap/customCtrlResources/formEinvoiceCtrlResources/html/" + configInformation[1];
        FileUtil.generateFile(configHtml + "/" + className + ".html", "/demo/init/controls/cap4/CAP4ConfigurationPage.txt",
            str -> str);

        // xxPC.js
        String jsPath = Objects.requireNonNull(e.getProject()).getBasePath() + "/" + configInformation[2] + "/apps_res/" + configInformation[1] + "/controls/js";
        FileUtil.generateFile(jsPath + "/" + className + ".js", "/demo/init/controls/cap4/CAP4PlusPCJs.txt",
            str -> str.replace("$controlId$", controlId));

        // xxMobile.js
        String jsMobilePath = Objects.requireNonNull(e.getProject()).getBasePath() + "/" + configInformation[2] + "/m3/apps/v5/meeting/" + configInformation[1] + "/js";
        FileUtil.generateFile(jsMobilePath + "/" + className + ".js", "/demo/init/controls/cap4/CAP4PlusMobileJs.txt", str -> str.replace("$controlId$", controlId));

        // spring-xx-controls.xml
        String xmlPath = Objects.requireNonNull(e.getProject()).getBasePath() + "/" + configInformation[2] + "/WEB-INF/cfgHome/plugin/" + configInformation[1] + "/spring";
        String filePath = xmlPath + "/spring-" + configInformation[1] + "-controls.xml";
        FileUtil.generateFile(filePath, "/demo/spring/spring-demo.txt",
            str -> ActionUtil.appendNewBean(str, filePath, information, className));
        VirtualFileManager.getInstance().refreshWithoutFileWatcher(false);
      }, "控件类名: ", "控件Id", "控件名称");
      SwingUtil.setTips(null);
    } else {
      Messages.showMessageDialog(
          e.getProject(),
          "The seeyon configuration file is required, but the corresponding file is not found or the file is incorrect, please reset it!",
          "Error",
          HToolIcon.ModuleIcon);
    }
  }

  @Override
  public void update(@NotNull AnActionEvent event) {
    ActionUtil.ActionP(event);
  }
}
